using System;
using System.Data;
using System.Data.SqlClient;

using PHSRAG.Insight21.BusinessObjects;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;

namespace PHSRAG.Insight21.ClinicalTrials
{
   /// <summary>
   /// The BudgetTask class is the facade over a single Budget Task
   /// </summary>
   public class BudgetTask : BusinessObject
   {

      #region Constants
      private const string BudgetTaskKey = "BudgetTask_F52323F3-AE23-4135-BE10-4006E4B47755";
      private const string TableName = "BudgetTask";
      #endregion

      #region Instance Variables
      private DataRow dataRow;
      private BudgetMilestoneVisits budgetMilestoneVisits;
      #endregion

      #region Public Properties
      /// <summary>
      /// Unique Id for this Budget Task
      /// </summary>
      public int Id
      {
         get
         {
            object o = dataRow["Id"];
            return (o == DBNull.Value) ? 0 : (int)o;
         }
      }

      /// <summary>
      /// Budget relate Id
      /// </summary>
      public int BudgetId
      {
         get
         {
            return (int)dataRow["ClinicalTrialBudgetId"];
         }
         set
         {
            dataRow["ClinicalTrialBudgetId"] = value;
         }
      }

      /// <summary>
      /// Budget staff Id
      /// Need to return a valid int back to the BudgetTask control to setup
      /// the person dropdown.
      /// </summary>
      public int BudgetStaffId
      {
         get
         {
            object o = dataRow["ClinicalTrialBudgetStaffId"];
            return ( o == DBNull.Value ) ? 0 : (int)o;
         }
         set
         {
            dataRow["ClinicalTrialBudgetStaffId"] = value;
         }
      }

      /// <summary>
      /// Budget task code
      /// </summary>
      public string BudgetTaskCode
      {
         get
         {
            return dataRow["BudgetTaskCode"].ToString( );
         }
         set
         {
            dataRow["BudgetTaskCode"] = value;
         }
      }

      /// <summary>
      /// Hours
      /// </summary>
      public string Hours
      {
         get
         {
            return dataRow["Hours"].ToString( );
         }
         set
         {
            dataRow["Hours"] = value;
         }
      }

      /// <summary>
      /// Other task text
      /// </summary>
      public string OtherTaskText
      {
         get
         {
            object o = dataRow["OtherTaskText"];
            return (o == DBNull.Value) ? null : o.ToString( );
         }
         set
         {
            dataRow["OtherTaskText"] = value;
         }
      }

      /// <summary>
      /// task cost
      /// </summary>
      public string TaskCost
      {
         get
         {
            object o = dataRow["TaskCost"];
            return (o == DBNull.Value) ? null : o.ToString( );
         }
         set
         {
            dataRow["TaskCost"] = value;
         }
      }

      /// <summary>
      /// Boolean indicating whether to apply overhead to this expense
      /// </summary>
      public bool ApplyOverhead
      {
         get
         {
            object o = dataRow["ApplyOverhead"];
            return ( o == DBNull.Value ) ? false : (bool)o;
         }
         set
         {
            dataRow["ApplyOverhead"] = value;
         }
      }

	/// <summary>
	/// Boolean indicating if overriding the rate times hour calculation
	/// </summary>
	   public bool OverRideRate
	   {
		   get
		   {
			   object o = dataRow["OverRideRate"];
			   return (o == DBNull.Value ) ? false : (bool)o;
      	   }
		   set 
		   {
			   dataRow["OverRideRate"] = value;
		   }

	   }


      /// <summary>
      /// Gets the BudgetMilestoneVisits associated with this Task
      /// </summary>
      public BudgetMilestoneVisits BudgetMilestoneVisits
      {
         get
         {
            if ( budgetMilestoneVisits == null )
            {
               budgetMilestoneVisits = new BudgetMilestoneVisits( CachePolicy );
               budgetMilestoneVisits.Load( Utility.DataAccess.GetConnectionString( ), "BudgetTask", Id );
            }
            return budgetMilestoneVisits;
         }
      }
      #endregion

      #region Constructor
      /// <summary>
      /// Initialize the base class with the specified cache policy and sets the dataRow member.
      /// </summary>
      public BudgetTask( ICachePolicy cachePolicy, DataRow row ) : base ( cachePolicy )
      {
         dataRow = row;
      }
      #endregion

      #region Base Methods
      /// <summary>
      /// Cache this instance using the prevalent cache policy.
      /// </summary>
      public override void Cache( )
      {
         CachePolicy.Cache( BudgetTaskKey, this );
      }

      /// <summary>
      /// Remove this instance from the cache using the prevalent cache policy.
      /// </summary>
      public override void UnCache( )
      {
         CachePolicy.UnCache( BudgetTaskKey );
      }

      /// <summary>
      /// Load method is not used
      /// </summary>
      /// <param name="connectionString"></param>
      /// <param name="args"></param>
      public override void Load( string connectionString, params object[] args )
      {
         throw new Exception("BudgetTask is not directly loadable");
      }
      #endregion

   }// END CLASS DEFINITION BudgetTask
}